package com.qumitech.visualnovel.service.virtualSearch.impl;

import com.alibaba.fastjson.JSONObject;

import com.qumitech.visualnovel.datasource.console.VisualConnectConfigMapper;
import com.qumitech.visualnovel.datasource.console.VisualSearchRecordMapper;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.dao.console.VisualConnectConfig;
import com.qumitech.visualnovel.model.dao.console.VisualSearchRecord;
import com.qumitech.visualnovel.service.virtualSearch.VisualSearchService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class VisualSearchServiceImpl implements VisualSearchService {

    @Autowired
    VisualConnectConfigMapper visualConnectConfigMapper;

    @Autowired
    VisualSearchRecordMapper visualSearchRecordMapper;

    @Override
    public List<HashMap<String,Object>> databaseOption() {
        return visualConnectConfigMapper.getOptions();
    }

    public VisualConnectConfig getVisualConnectConfigById(Integer Id)
    {
        return visualConnectConfigMapper.selectByPrimaryKey(Id);
    }

    @Override
    public int update(VisualSearchRecord visualSearchRecord) {
        return visualSearchRecordMapper.updateByPrimaryKeySelective(visualSearchRecord);
    }

    @Override
    public List<VisualSearchRecord> getList(Integer id, String title, Integer status, String startTime, String endTime, Integer offset, Integer limit, String orderBy, String orderType) {
        return visualSearchRecordMapper.getList(id, title, status, startTime, endTime, offset, limit, orderBy, orderType);
    }

    @Override
    public Integer getCount(Integer id, String title, Integer status, String startTime, String endTime) {
        return visualSearchRecordMapper.getCount(id, title, status, startTime, endTime);
    }

    @Override
    public VisualSearchRecord addTask(JSONObject param) {
        Integer databaseId = param.getInteger("databaseId");
        String sql = param.getString("taskSql");
        String remark = param.getString("taskName");
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        String userName = activeUser.getUsername();
        VisualSearchRecord visualSearchRecord = new VisualSearchRecord();
        visualSearchRecord.setDatabaseId(databaseId);
        visualSearchRecord.setStatus(0);
        visualSearchRecord.setPerson(userName);
        visualSearchRecord.setTaskSql(sql);
        visualSearchRecord.setTaskName(remark);visualSearchRecordMapper.insertSelective(visualSearchRecord);
        return visualSearchRecord;
    }

    public VisualSearchRecord findVisualSearchRecordById(Integer Id)
    {
        return visualSearchRecordMapper.selectByPrimaryKey(Id);
    }
}
